home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1 / Nebula One.iso / Utilities / Workspace / WrapperInspector / Source / TextSubInspector.m < prev    next >
Text File  |  1995-06-12  |  4KB  |  170 lines

  1. //----------------------------------------------------------------------------------------------------
  2. //
  3. //    TextSubInspector
  4. //
  5. //    Inherits From:        DefaultSubInspector
  6. //
  7. //    Declared In:        TextSubInspector.h
  8. //
  9. //    Disclaimer
  10. //
  11. //        You may freely copy, distribute and reuse this software and its
  12. //        associated documentation. I disclaim any warranty of any kind, 
  13. //        expressed or implied, as to its fitness for any particular use.
  14. //
  15. //----------------------------------------------------------------------------------------------------
  16. #import "TextSubInspector.h"
  17. #import <appkit/appkit.h>
  18.  
  19.  
  20. @implementation TextSubInspector
  21.  
  22. static id     _SELF = nil;
  23. static id    font;
  24.  
  25. //----------------------------------------------------------------------------------------------------
  26. //  Initialization and Free Methods
  27. //----------------------------------------------------------------------------------------------------
  28. + new
  29. {
  30.     // Only allow one instance... 
  31.         
  32.         if (_SELF) return _SELF;
  33.         _SELF = self = [super new];
  34.     font = [Text getDefaultFont];
  35.      return _SELF;
  36. }
  37.  
  38.  
  39. //----------------------------------------------------------------------------------------------------
  40. //  Private Methods
  41. //----------------------------------------------------------------------------------------------------
  42. - (STR) extension: (const char*) path
  43. {
  44.     //  File's extension if found, pointer to NULL otherwise.  Caller must free.
  45.  
  46.     STR    start = (STR)path;
  47.     STR    end;
  48.  
  49.     for (end = (start + strlen(start) -1); end >= start; end--)
  50.         {
  51.         if (*end == '.') break;
  52.         if (*end == '/') return NULL;
  53.         }
  54.  
  55.     if ((end - start) == -1) return NULL;
  56.         
  57.     start += (end - start) + 1;
  58.     return NXCopyStringBuffer(start);
  59. }
  60.  
  61.  
  62. - (BOOL)isRTFDStream: (STR) path
  63. {
  64.     //  An RTFD can be either a typed stream or a directory containing
  65.     //  graphics and an rtf.  Text uses different methods to access these
  66.     //  (and craters if you use the wrong one).
  67.     
  68.     struct stat    statBuffer;
  69.     
  70.     stat (path, &statBuffer);
  71.     
  72.     if ( (statBuffer.st_mode & S_IFMT) != S_IFDIR)
  73.         return YES;
  74.         
  75.     return NO;
  76. }
  77.  
  78.     
  79. //----------------------------------------------------------------------------------------------------
  80. //  Inspection Methods
  81. //----------------------------------------------------------------------------------------------------
  82. - inspect: (STR) path
  83. {
  84.     STR        extension;
  85.         
  86.     if (! path) return self;
  87.  
  88.     if ( ! (extension = [self extension: path])) return self;
  89.  
  90.     [text setFont: font];
  91.     [text setHorizResizable: YES];
  92.     [text setVertResizable: YES];
  93.     [[[text superview] superview] setHorizScrollerRequired: YES];
  94.  
  95.     if (NXOrderStrings(extension, "rtf", YES, -1, NULL) == 0)
  96.         [self readRTF: path];
  97.     else if (NXOrderStrings(extension, "rtfd", YES, -1, NULL) == 0)
  98.         [self readRTFD: path];
  99.     else
  100.         [self readText: path];
  101.         
  102.     [text sizeToFit];
  103.     return self;
  104. }
  105.  
  106.  
  107. //----------------------------------------------------------------------------------------------------
  108. //  Read Methods        
  109. //----------------------------------------------------------------------------------------------------
  110. - readRTF: (STR)path
  111. {
  112.     //  Read an RTF file.
  113.     
  114.     NXStream* stream = NXMapFile (path, NX_READONLY);
  115.     if (! stream) return [self inspectionError: path];
  116.     [text readRichText: stream];
  117.     NXClose(stream);
  118.     return self;
  119. }
  120.  
  121.  
  122. - readRTFD: (STR)path
  123. {
  124.     //  Read an RTFD file or stream.
  125.  
  126.     NXRTFDError openStatus;
  127.  
  128.     if ([self isRTFDStream: path])
  129.         {
  130.         NXStream* stream = NXMapFile (path, NX_READONLY);
  131.         if (! stream) return [self inspectionError: path];
  132.         [text readRTFDFrom: stream];
  133.         NXClose(stream);
  134.         }
  135.     else
  136.         {
  137.         openStatus = [text openRTFDFrom: path];
  138.         if (openStatus != NX_RTFDErrorNone) return [self inspectionError: path];
  139.         }
  140.         
  141.     return self;
  142. }
  143.  
  144.  
  145. - readText: (STR)path;
  146. {
  147.     //  Read a (hopefully) text file.
  148.     
  149.     NXStream* stream = NXMapFile (path, NX_READONLY);
  150.     if (! stream) return [self inspectionError: path];
  151.     [(Text*)text readText: stream];
  152.     NXClose(stream);
  153.     return self;
  154. }
  155.  
  156.  
  157. //----------------------------------------------------------------------------------------------------
  158. //  Accessing Inspection View
  159. //----------------------------------------------------------------------------------------------------
  160. - clearInspectorView
  161. {
  162.     //  Clear view...
  163.     
  164.     [text setText: ""];
  165.     [text setSel:0 :0];
  166.     return self;
  167. }
  168.  
  169.  
  170. @end